package ui
{
	import events.GCEvent;
	import ui.button.GCButton;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import org.flixel.FlxG;
	
	/**
	 * An individual scroll part for the larger GCNumberScroller
	 * component.
	 */ 
	public class GCNumberScrollPart extends Sprite
	{
		private static const COMPONENT_WIDTH:int = 20;
		private static const COMPONENT_HEIGHT:int = 20;
		
		private var _value:int;
		private var _numberDisplay:TextField;
		private var _increaseButton:GCButton;
		private var _decreaseButton:GCButton;
		
		/**
		 * Constructor
		 */
		public function GCNumberScrollPart()
		{
			super();
			
			_value = 0;
			_numberDisplay = new TextField();		

			_increaseButton = new GCButton(onIncreaseValue);
			_increaseButton.loadGraphic(GCResources.up_arrow, 20, 20);
			_decreaseButton = new GCButton(onDecreaseValue);
			_decreaseButton.loadGraphic(GCResources.down_arrow, 20, 20);
			
			this.init();
		}
		
				private function init():void
		{
			_numberDisplay.text = String(this._value);
			
			_increaseButton.x = 0;
			_increaseButton.y = 0;
			
			_decreaseButton.x = 0;
			_decreaseButton.y = COMPONENT_HEIGHT + _increaseButton.height;
			
			_numberDisplay.background = true;
			_numberDisplay.x = 0;
			_numberDisplay.y = _increaseButton.height;
			_numberDisplay.width = COMPONENT_WIDTH;
			_numberDisplay.height = COMPONENT_HEIGHT;
			_numberDisplay.selectable = false;
			
			this.addChild(_increaseButton);
			this.addChild(_decreaseButton);
			this.addChild(_numberDisplay);
		}
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------
		/**
		 * Set the value for the single scroller part.
		 * 
		 * @param value
		 * @param doDispatch
		 * 		Boolean indicating whether we want to dispatch an event indicating
		 * 		that part changed.
		 */
		public function setValue(value:int, doDispatch:Boolean = true):Boolean
		{
			/*
			if (value >= MIN_VALUE && value <= _upperLimit)
			{
				this._value = value;
				_numberDisplay.text = String(this._value);
				
				if (doDispatch)
				{
					dispatchEvent(new Event(VALUE_CHANGED_EVENT));
				}
				
				return true;
			}
			
			return false;*/
			if (value >= 0 && value < 10)
			{
				this._value = value;
				_numberDisplay.text = String(this._value);
				return true;
			}
			else 
				return false;
		}
		
		/**
		 * Set the x and y coordinates for this piece
		 */
		public function setXY(x:int, y:int):void
		{
			this.x = x;
			this.y = y;
		}
		
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------		
		
		public function get value():int
		{
			return this._value;
		}
		
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------
		
		private function onIncreaseValue(event:MouseEvent):void
		{
			// New value equals 0 if old value is 9 otherwise it equals old value + 1			
			var newValue:int = (_value + 1 > 9)?0:_value + 1;
			setValue(newValue);
			dispatchEvent(new Event(GCEvent.VALUE_INCREASE_EVENT));
		}
		
		private function onDecreaseValue(event:MouseEvent):void
		{
			// New value equals 9 if old value was 0 otherwise it equals old value - 1
			var newValue:int = (_value-1 < 0)?9:_value-1;
			setValue(newValue);
			dispatchEvent(new Event(GCEvent.VALUE_DECREASE_EVENT));
		}
	}
}